package com.damionew.website.controller.back;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.damionew.website.model.back.Member;
import com.damionew.website.service.back.MemberService;
import com.github.pagehelper.PageHelper;

@Controller
@RequestMapping("/member")
public class MemberController {
	    @Autowired
	    private MemberService memberService;
	    //指向管理员管理页面
	    @RequestMapping("/index")
	    public String index(Member member) {
		System.out.println("指向管理员管理页面");
		return "back/member/index";
	    }
	  //指向管理员管理页面
	    @RequestMapping("/loginindex")
	    public String loginindex() {
		return "back_login";
	    }
	    //分页查询
	    @ResponseBody
	    @RequestMapping(value="pageInfo",produces="html/text;charset=UTF-8")
	    public  String pageInfo(@RequestParam int pageNumber,int pageSize,HttpServletResponse response,String sortName,String sortOrder) {
		//数据总数，返回到前台总记录数，pageInfo为一页的数量，不可信
		List<Member> members =memberService.getMemberList(sortName, sortOrder);
		int total = members.size();
		//startPage方法传入参数(初始页码，一页数量)，只对跟着此方法的下一条查询有效
		PageHelper.startPage(pageNumber,pageSize);
		List<Member> pageInfo=memberService.getMemberList(sortName,sortOrder);
		//json返回每页数据和数据总数
		JSONObject result = new JSONObject();
		result.put("rows",pageInfo);
		result.put("total",total);
		return result.toJSONString();
	    }
	  //新增用户
	    @ResponseBody
	    @RequestMapping("/addMember")
	    public String addMember(Member member) {
		//这里添加创建时间
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = dateFormat.format(date);
		member.setCreated_at(time);
		//初始设置状态为1，即启用状态
		memberService.addMember(member);
		//返回前台ajax，json：{"state":"success"},success方法调用提示框
		JSONObject result = new JSONObject();
		result.put("state", "success");
		return result.toJSONString();
	    }
	    //修改用户
	    @ResponseBody
	    @RequestMapping("/updateMember")
	    public String updateMember(Member member) {
		memberService.updateMember(member);
		JSONObject result = new JSONObject();
		result.put("state", "success");
		return result.toJSONString();
	    }
	    //删除用户
	    @ResponseBody
	    @RequestMapping("/deleteMember")
	    public String deleteMember(HttpServletRequest request) {
		//获取前台传送的数组，并for循环删除
		String[] list=request.getParameterValues("ids");
		for (int i = 0; i < list.length; i++) {
		    String id = list[i];
		    memberService.deleteMember(id);
		}
		JSONObject result = new JSONObject();
		result.put("state", "success");
		return result.toJSONString();
	    }
}

